
class UserInfo {

    userId = ""
    isSubscribe   = false
    // use the var when the subscribe is true
    localRtcConnection = null
    peerRtcConnection = null
    userCandidateArray = []
    // srfix type candidate
    selfCandidateArray = []
    status = false
}

let UserList = []

function UserInReq(room_id, user_id) {

    let userInReqMsg = {

        "header":{

            "msg_type":"user_in_req",
            "room_id":room_id,
            "user_id":user_id
        },
        "content":{

        }
    }
    return JSON.stringify(userInReqMsg)
}

function UserOutReq(room_id, user_id) {

    let message = {

        "header":{

            "msg_type":"user_out",
            "room_id":room_id,
            "user_id":user_id
        },
        "content":{

        }
    }
    return JSON.stringify(message)
}

function ProcessUserInReq(roomReferenceData, jsonMessage) {

    if (jsonMessage.header.user_id === roomReferenceData.userId)
    {
        PublishStreamStatus().then(result=>{

            console.log("function is " + result)
            // login signal room success
            console.log("finish login room")
            roomReferenceData.loginStatus = true

            for (let index= 0; index < jsonMessage.content.user_list.length; index++)
            {
                let user = new UserInfo()
                user.userId = jsonMessage.content.user_list[index]
                user.isSubscribe = false
                user.localRtcConnection = null
                user.peerRtcConnection  = null
                roomReferenceData.newUserList.push(user)

                // get publish stream status
                let message = GetPublishStreamStatusReq(roomReferenceData, user.userId)
                PostMessageToWebsocket(message)
            }
        })
    }
    else
    {
        alert("error connection")
    }
}

function ProcessUserInBcast(roomReferenceData, message)
{
    if (message.content.user_id === roomReferenceData.userId )
    {
        // let msg = GetPublishList(roomReferenceData)
        // PostMessageToWebsocket(msg)
        // publish
    }
    else
    {
        // save user list
        let user = new UserInfo()
        user.userId = message.content.user_id;
        user.isSubscribe = false
        user.localRtcConnection = null
        user.peerRtcConnection  = null
        roomReferenceData.newUserList.push(user)

        console.log(roomReferenceData)

        //CreateLocalPeerConnection(roomReferenceData, user.userId).then()
    }
}

function ProcessUserOut(roomReferenceData, message) {

    if (message.content.user_id !== roomReferenceData.userId)
    {
        let user_id = message.content.user_id
        console.log("[user_out] user_id : " + user_id)
        for (let index in roomReferenceData.newUserList)
        {
            let user = roomReferenceData.newUserList[index]
            if (user.userId === user_id)
            {
                StopSubscribeStream(roomReferenceData, user_id)
                roomReferenceData.newUserList.splice(user, 0)
                break
            }
        }
    }
}